// Haxzs Audio Server
// made by omghaxzs
// 2010

#include "sourcewnd.h"

sourcewnd::sourcewnd(int y, int x, int width, int height):window(y, x, width, height)
{
	cursory = 1;
	maxlines = height-2;
	// Initialize curses screen
	if(has_colors() == FALSE)
	{	
		endwin();
		printf("Your terminal does not support color\n");
		exit(1);
	}

	this->y = y;
	this->x = x;
	this->width = width;
	this->height = height;

	// Create the window
	resetWindow();
}

sourcewnd::~sourcewnd()
{
	endwin();
}

void sourcewnd::resetWindow()
{
	// Create a new window
	win = newwin(height, width, y, x);
	box(win, 0, 0);
	wmove(win, 2, 0);
	waddch(win, ACS_LTEE);
	for(int i=0; i<23; i++)
	{
		waddch(win, ACS_HLINE);
	}
	waddch(win, ACS_RTEE);

	wrefresh(win);

	// Initially set the cursor
	wmove(win, 1, 1);

	// Write "No Source Connected!"
	wprintw(win, "No Source Connected!");
	wrefresh(win);
}

void sourcewnd::updateSourceInfo(std::string agent, std::string type, std::string rate)
{
	// Print the Agent
	wmove(win, 3, 1);
	wprintw(win, agent.c_str());

	// Then the rate
	wmove(win, 4, 1);
	wprintw(win, rate.c_str());

	// Then the type
	wmove(win, 5, 1);
	wprintw(win, type.c_str());

	// Refresh the window
	wrefresh(win);
}

void sourcewnd::updateSourceBytes(int bytes)
{
	wmove(win, 6, 1);

	std::stringstream Num;
	std::string str;
	Num << bytes;
	str = Num.str();
	str += " bytes";
	wprintw(win, str.c_str());

	wrefresh(win);
}

void sourcewnd::sourceConnected(bool connected)
{
	wmove(win, 1, 1);
	if (connected)
	{
		wprintw(win, "                       ");
	}
	else
	{
		wprintw(win, "No Source Connected!");
	}
}

void sourcewnd::updateSourceIP(std::string ip)
{
	wmove(win, 1, 1);
	wprintw(win, ip.c_str());
	wrefresh(win);
}

void sourcewnd::updateSourceCache(std::string cache)
{
	std::string str;

	str = "Cache: ";
	str += cache;
	str += "%";
	wmove(win, 7, 1);
	wprintw(win, str.c_str());
	wrefresh(win);
}

void sourcewnd::clearSourceInfo()
{
	// Just reset the window
	resetWindow();
}
